//
//  QNetWorkRequest.m
//  quxiangtao
//
//  Created by HHuajiang on 2018/11/12.
//  Copyright © 2018 shanhaijin. All rights reserved.
//

#import "QNetWorkRequest.h"
#import "PPNetworkHelper.h"
@implementation QNetWorkRequest

/**
 淘宝商品详情
 */
+ (NSURLSessionTask *)taoBaoShopDetailarameter:(id)parameter Success:(PPRequestSuccess)success Failure:(PPRequestFailure)failure {
    return [self qpostRequestWithURL:@"goods/details/appdetails" parameters:parameter success:success failure:failure];
}

/**
 自营页面弹窗
 */
+ (NSURLSessionTask *)autotrophySuccess:(PPRequestSuccess)success {
    return [self qpostRequestWithURL:@"index/Link/index" parameters:nil success:success failure:nil];
}

/**
 京东商品分类
 */
+ (NSURLSessionTask *)jdongCommodityClassificationSuccess:(PPRequestSuccess)success {
    return [self qpostRequestWithURL:@"goods/jdgoods/getClass" parameters:nil success:success failure:nil];
}

/**
 京东商品列表
 */
+ (NSURLSessionTask *)jdongShopingLisParameter:(id)parameter Success:(PPRequestSuccess)success Failure:(PPRequestFailure)failure {
    return [self qpostRequestWithURL:@"goods/jdgoods/getGoodsInfo" parameters:parameter success:success failure:failure];
}

/**
 意见反馈
 */
+ (NSURLSessionTask *)feedbackparameter:(id)parameter Success:(PPRequestSuccess)success Failure:(PPRequestFailure)failure {
    return [self noEncryptionpostRequestWithURL:@"user/Feedback/index" parameters:parameter success:success failure:failure];
}

#pragma mark - 请求的公共方法
+ (NSURLSessionTask *)qpostRequestWithURL:(NSString *)URL parameters:(NSDictionary *)parameter success:(PPRequestSuccess)success failure:(PPRequestFailure)failure
{
    NSMutableDictionary *dicationary = [NSMutableDictionary dictionary];
    [parameter enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSString * _Nonnull obj, BOOL * _Nonnull stop) {
        [dicationary setValue:[RSA encryptString:obj publicKey:PublicKey] forKey:key];
    }];
    NSData *dataJson = [NSJSONSerialization dataWithJSONObject:dicationary options:0 error:NULL];
    NSString *jsonStr = [[NSString alloc] initWithData:dataJson encoding:NSUTF8StringEncoding];
    return [PPNetworkHelper POST:URL parameters:@{@"json":jsonStr} success:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 1) {
            success(responseObject);
        } else {
            [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"msg"]];
        }
    } failure:^(NSError *error) {
        if (failure) {
           failure(error);
        }
    }];
}

#pragma mark - 请求的公共方法
+ (NSURLSessionTask *)noEncryptionpostRequestWithURL:(NSString *)URL parameters:(NSDictionary *)parameter success:(PPRequestSuccess)success failure:(PPRequestFailure)failure
{
//    NSMutableDictionary *dicationary = [NSMutableDictionary dictionary];
//    [parameter enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSString * _Nonnull obj, BOOL * _Nonnull stop) {
//        [dicationary setValue:[RSA encryptString:obj publicKey:PublicKey] forKey:key];
//    }];
    NSData *dataJson = [NSJSONSerialization dataWithJSONObject:parameter options:0 error:NULL];
    NSString *jsonStr = [[NSString alloc] initWithData:dataJson encoding:NSUTF8StringEncoding];
    return [PPNetworkHelper POST:URL parameters:@{@"json":jsonStr} success:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 1) {
            success(responseObject);
        } else {
            [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"msg"]];
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}


@end
